package com.zimo.gui;

import com.zimo.download.DownloadCacheImpl;
import com.zimo.download.DownloadImpl;
import com.zimo.download.exception.DownloadException;
import com.zimo.download.info.ServerFileINFO;
import com.zimo.download.info.event.EventSet;
import com.zimo.download.thread.ThreadManageDownload;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class DownloadR extends ThreadManageDownload {

    /**
     * 已经下载完成的任务
     */
    public final int FINISHED = 0;
    /**
     * 所有的下载任务
     */
    public final int ALL = -1;
    /**
     * 正在下载中的任务
     */
    public final int DOWNLOADING = 1;


    private static DownloadR downloadR;
    private DownloadR (){}
    /**
     * 获取 DownloadR 实例
     */
    public static synchronized DownloadR getDownloadR() {
        if (downloadR == null) {
            downloadR = new DownloadR();
        }
        return downloadR;
    }

    /**
     * 下载的实例,存放了所有的下载任务
     */
    private ArrayList<ThreadManageDownload> downloads = new ArrayList<>();

    /**
     * 列出下载任务
     * @param state 下载状态 (DownloadR常量)
     * @return
     */
    public ArrayList<ThreadManageDownload> list(int state){
        ArrayList<ThreadManageDownload> objects = new ArrayList<>();
        if (state <= -1){
            return downloads;
        }else if (state == 0){
            //如果完成的线程数大于等于预先设置的文件最大下载线程数，就放入obj集合
            for (ThreadManageDownload download : downloads) if (download.getThreadCount() <= download.getFinishThread()) objects.add(download);
        }else{
            for (ThreadManageDownload download : downloads) if (download.getThreadCount() > download.getFinishThread()) objects.add(download);
        }
        clearAll();
        return objects;
    }

    /**
     * 移除下载完成(包括下载失败的和暂停下载的)的tim文件
     */
    public void clearAll(){
        for (ThreadManageDownload download : downloads)
            if (download.getThreadCount() <= download.getFinishThread()) DownloadCacheImpl.clear(download.getFilePath(),getFileName());
    }

    /**
     * 移除tim文件
     */
    public void clear(ThreadManageDownload download){
        DownloadCacheImpl.clear(download.getFilePath(),getFileName());
    }

    private File getTime(String fileName){
        return null;
    }
    private File[] getTime(){
        return null;
    }

    /**
     * 开始下载
     * @param url   服务器地址
     * @param filePath  保存地址
     * @param threadCount   下载线程数
     * @return 是否开始了下载
     * @throws IOException
     */
    public boolean start(URL url, String filePath,int threadCount) throws IOException, DownloadException {
        //如果磁盘空间小于下载文件+20MB就禁止下载
        if (new File(filePath).getFreeSpace() < ServerFileINFO.getFileSize(url)+20*1024*1024)
            throw new DownloadException("磁盘空间不足");

        //禁止重复的url下载，否则后者会覆盖前者
        for (ThreadManageDownload download : downloads) {
           if ( download.getUrl().toString().equals(url.toString())) throw new DownloadException("url重复，文件被重复下载了: "+url);
        }

        //初始化
        ThreadManageDownload download = new ThreadManageDownload();//创建下载线程管理器器
        download.setDownload(new DownloadImpl());//选择多线程下载器
        download.setUrl(url);//下载地址 URL
        download.setFilePath(filePath);//保存地址
        download.setThreadCount(threadCount);//线程数
        download.setFileName(ServerFileINFO.getFileName(url));//文件保存名称

        //管理器放入集合
        downloads.add(download);
        return  download.startDownload();//开始下载
    }

    /**
     * 暂停任务
     * @param url
     * @return
     * @throws IOException
     */
    public boolean stop(URL url) throws IOException {
        for (ThreadManageDownload download : downloads)
            if (download.getUrl().toString().equals(url.toString())) return download.stopDownload(ServerFileINFO.getFileName(url));
        return false;
    }

    /**
     * 暂停任务
     * @param fileName
     * @return
     */
    public boolean stop(String fileName){
        for (ThreadManageDownload download : downloads)
            if (download.getFileName().equals(fileName)) return download.stopDownload(fileName);
        return false;
    }


    /**
     * 暂停所有的下载任务
     * @return
     */
    public boolean stop(){
        for (ThreadManageDownload download : downloads) download.stopDownload(download.getFileName());
        return true;
    }

    /**
     * 续传
     * @param timFilePath  缓存文件路径
     * @return
     * @throws IOException
     */
    public boolean continuation(String timFilePath) throws IOException {
        ThreadManageDownload download = new ThreadManageDownload();
        downloads.add(download);
        return download.continuation(timFilePath);
    }


    /**
     * 移出线程下载管理器
     * @param threadManageDownload 管理器实例，请通过list获取
     * @return
     * @throws IOException
     */
    public boolean remove(ThreadManageDownload threadManageDownload) throws IOException {
        threadManageDownload.stopDownload(threadManageDownload.getFileName());
        downloads.remove(threadManageDownload);
        EventSet.getEventSet().remove(threadManageDownload.getUrl());
        return true;
    }

    /**
     * 获取已经下载的文件大小
     * @return
     */
    public long getDownloadingFileSize(ThreadManageDownload threadManageDownload) {
        return threadManageDownload.getDownloadingFileSize();
    }

    /**
     * 获取文件大小
     * @return
     */
    public long getFileSize(ThreadManageDownload threadManageDownload) throws IOException {
        return ServerFileINFO.getFileSize(threadManageDownload.getUrl());

    }

    /**
     *  废弃
     * @return
     */
    @Deprecated
    public long getDownloadingFileSize() {
        return -1;
    }


}
